package com.qf.purchasing.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.purchasing.mapper.AuditMapper;
import com.qf.purchasing.qo.AuditQueryQO;
import com.qf.purchasing.service.AuditService;
import com.qf.purchasing.vo.AuditVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AuditServiceImpl implements AuditService {
    @Resource
    private AuditMapper auditMapper;

    @Override
    public RespResult<PageInfo<AuditVO>> query(int page, int size, AuditQueryQO auditQueryQO) {
        PageInfo<AuditVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(
                () -> auditMapper.selectList(auditQueryQO)
        );
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return RespResult.success(ResultCode.SUCCESS, pageInfo);
    }


    @Override
    public RespResult<String> auditByIds(int[] auditIds) {
        int count = auditMapper.updateIsAudit(auditIds);
        if (count == auditIds.length) {
            return RespResult.success("审核成功！");
        } else if (count < 1) {
            throw new ServiceException(ResultCode.AUDIT_ERROR);
        } else {
            return RespResult.success("数据有误，部分审核成功！");
        }
    }

    @Override
    public RespResult<List<AuditVO>> queryAuditList() {
        List<AuditVO> auditVOS = auditMapper.selectAuditList();
        if (auditVOS.isEmpty()) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return RespResult.success(ResultCode.SUCCESS, auditVOS);
    }

    @Override
    public RespResult<List<AuditVO>> queryNotAuditList() {
        List<AuditVO> auditVOS = auditMapper.selectNotAuditList();
        if (auditVOS.isEmpty()) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return RespResult.success(ResultCode.SUCCESS, auditVOS);
    }


}
